<?php

// 应用公共文件,内置主要的数据处理方法
use think\facade\Config;
use think\facade\Request;
use think\facade\Cache;
use think\facade\Db;
use \think\facade\Log;
//获取后台模块当前登录用户的信息
function get_login_admin($key = "")
{
    $session_admin = get_config('app.session_admin');
    if (\think\facade\Session::has($session_admin)) {
        $gougu_admin = \think\facade\Session::get($session_admin);
        if (!empty($key)) {
            if (isset($gougu_admin[$key])) {
                return $gougu_admin[$key];
            } else {
                return '';
            }
        } else {
            return $gougu_admin;
        }
    } else {
        return '';
    }
}
/**
 * 截取摘要
 *  @return bool
 */
function getDescriptionFromContent($content, $count)
{
    $content = preg_replace("@<script(.*?)</script>@is", "", $content);
    $content = preg_replace("@<iframe(.*?)</iframe>@is", "", $content);
    $content = preg_replace("@<style(.*?)</style>@is", "", $content);
    $content = preg_replace("@<(.*?)>@is", "", $content);
    $content = str_replace(PHP_EOL, '', $content);
    $space = array(" ", "　", "  ", " ", " ");
    $go_away = array("", "", "", "", "");
    $content = str_replace($space, $go_away, $content);
    $res = mb_substr($content, 0, $count, 'UTF-8');
    if (mb_strlen($content, 'UTF-8') > $count) {
        $res = $res . "...";
    }
    return $res;
}
/**
 * PHP格式化字节大小
 * @param number $size      字节数
 * @param string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '')
{
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++) {
        $size /= 1024;
    }

    return round($size, 2) . $delimiter . $units[$i];
}

function create_tree_list($pid, $arr, $group, &$tree = [])
{
    foreach ($arr as $key => $vo) {
        if ($key == 0) {
            $vo['spread'] = true;
        }
        if (!empty($group) and in_array($vo['id'], $group)) {
            $vo['checked'] = true;
        } else {
            $vo['checked'] = false;
        }
        if ($vo['pid'] == $pid) {
            $child = create_tree_list($vo['id'], $arr, $group);
            if ($child) {
                $vo['children'] = $child;
            }
            $tree[] = $vo;
        }
    }
    return $tree;
}


function city_create_tree_list($pid, $arr, $group, &$tree = [])
{
    foreach ($arr as $key => $vo) {
        if ($key == 0) {
            $vo['spread'] = true;
        }
        if (!empty($group) and in_array($vo['id'], $group)) {
            $vo['checked'] = true;
        } else {
            $vo['checked'] = false;
        }
        if ($vo['tid'] == $pid) {
            $child = city_create_tree_list($vo['id'], $arr, $group);
            if ($child) {
                $vo['children'] = $child;
            }
            $tree[] = $vo;
        }
    }
    return $tree;
}

//递归排序，用于分类选择
function set_recursion($result, $pid = 0, $level=-1)
{
    /*记录排序后的类别数组*/
    static $list = array();
    static $space = ['','├─','§§├─','§§§§├─','§§§§§§├─'];
	$level++;
    foreach ($result as $k => $v) {
        if ($v['pid'] == $pid) {
            if ($pid != 0) {
                $v['title'] = $space[$level] . $v['title'];
            }
            /*将该类别的数据放入list中*/
            $list[] = $v;
            set_recursion($result, $v['id'],$level);
        }
    }
    return $list;
}

/**
 * 根据id递归返回子数据
 * @param  $data 数据
 * @param  $pid 父节点id
 */
function get_data_node($data=[],$pid=0){
	$dep = [];
	foreach($data as $k => $v){
		if($v['pid'] == $pid){
			$node=get_data_node($data, $v['id']);
			array_push($dep,$v);
			if(!empty($node)){
				$dep=array_merge($dep,$node);
			}
		}
	}
	return array_values($dep);
}

//获取指定管理员的信息
function get_admin($id)
{
    $admin = Db::name('Admin')->where(['id' => $id])->find();
    $admin['group_id'] = Db::name('AdminGroupAccess')->where(['uid' => $id])->column('group_id');
    return $admin;
}

//读取权限节点列表
function get_admin_rule()
{
    $rule = Db::name('AdminRule')->where(['status'=>1])->order('sort asc,id asc')->select()->toArray();
    return $rule;
}

//读取城市节点列表
function get_citylist()
{
    $where=[];
    $where[]=['code','like','42%'];
    $citys = Db::name('citys')->where($where)->order('id asc')->select()->toArray();
    return $citys;
}

//读取模块列表
function get_admin_module()
{
    $group = Db::name('AdminModule')->order('id asc')->select()->toArray();
    return $group;
}

//读取权限分组列表
function get_admin_group()
{
    $group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray();
    return $group;
}
//读取权限分组列表-城市合伙人
function get_admin_group_hehuoren()
{
    $where=[];
    $where[]=['title','=','城市合伙人'];
    $group = Db::name('AdminGroup')->where($where)->order('create_time asc')->select()->toArray();
    return $group;
}

//读取指定citys权限分组详情
function get_admin_citys_info($id)
{
    $city = Db::name('Admin')->where(['id' => $id])->value('citys');
    $citys = explode(',', $city);
    return $citys;
}

//读取指定权限分组详情
function get_admin_group_info($id)
{
    $rule = Db::name('AdminGroup')->where(['id' => $id])->value('rules');
	$rules = explode(',', $rule);
    return $rules;
}

//读取导航列表,用于后台
function get_nav($nav_id)
{
    $nav = Db::name('NavInfo')->where('nav_id', $nav_id)->order('sort asc')->select();
    return $nav;
}

//读取关键字列表
function get_keywords()
{
    $keywords = Db::name('Keywords')->where(['status' => 1])->order('create_time asc')->select();
    return $keywords;
}

//读取文章分类列表
function get_article_cate()
{
    $cate = Db::name('ArticleCate')->order('create_time asc')->select()->toArray();
    return $cate;
}

//读取游戏app列表
function get_game_applist()
{
    $applist = Db::connect('guangying')->name('GameApp')->field('id,app_name')->where('is_delete',0)->order('id asc')->select()->toArray();
    //Log::info('applist='.json_encode($applist));
    return $applist;
}

function get_apkurl($id,$type)
{
    $files = Db::name('File')->field('id,filename,filepath')->where('id',$id)->where('delete_time',0)->order('id asc')->find();
    if ($files){
        if($type==1){
            return $files['filename'];
        }else{
            return $files['filepath'];
        }

    }else{
        return '';
    }
}

//获取版本名称和版本号
function get_versions($id)
{
    $versions = Db::name('Versions')->field('id,versions,name')->where('id',$id)->where('delete_time',0)->order('id asc')->find();
    if ($versions){
        return $versions;
    }else{
        return '';
    }
}

function get_shop_area($code)
{
    $areas = Db::name('citys')->field('id,name')->where('code',$code)->order('id asc')->find();
    if ($areas){
        return $areas['name'];
    }else{
        return '';
    }
}

//获取商家名称
function get_shop_name($isshop,$id)
{
    if($isshop==0){
        return '';
    }
    $shops = Db::name('user_shop')->field('id,shop_name')->where('id',$id)->order('id asc')->find();
    if ($shops){
        return $shops['shop_name'];
    }else{
        return '';
    }
}

//获取商家的用户id
function get_shop_uid($shopid)
{

    $shops = Db::name('user_shop')->field('id,uid')->where('id',$shopid)->order('id asc')->find();
    if ($shops){
        return $shops['uid'];
    }else{
        return '';
    }
}
//读取支付类型列表
function get_paystatus_list(){
    $sdktype = array(
        array('id'=>1,'name'=>'成功'),
        array('id'=>2,'name'=>'处理中'),
        array('id'=>3,'name'=>'失败'),
    );
    return $sdktype;
}
//读取兑换券有效期列表
function get_redcodemonth_list(){
    $monthlist = array(
        array('id'=>3,'name'=>'3个月'),
        array('id'=>4,'name'=>'4个月'),
        array('id'=>5,'name'=>'5个月'),
        array('id'=>6,'name'=>'6个月'),
    );
    return $monthlist;
}

//读取订单类型列表
function get_ordertype_list(){
    $sdktype = array(
        array('id'=>1,'name'=>'开通vip信息'),
        array('id'=>2,'name'=>'充值信息'),
        array('id'=>3,'name'=>'付款信息'),
        array('id'=>4,'name'=>'退款信息'),
        array('id'=>5,'name'=>'提现信息'),
    );
    return $sdktype;
}

//读取用户消息类型列表
function get_messagetype_list(){
    //文字消息：1、图片消息:2、语音消息:3
    $sdktype = array(
        array('id'=>1,'name'=>'文字消息'),
        array('id'=>2,'name'=>'图片消息'),
        array('id'=>3,'name'=>'语音消息'),
    );
    return $sdktype;
}

//读取sdk类型列表
function get_sdktype_list(){
    $sdktype = array(
        array('type_id'=>1,'type_name'=>'独立sdk'),
        array('type_id'=>2,'type_name'=>'官方SDK'),
    );
    return $sdktype;
}
//读取渠道类型列表
function get_channeltype_list(){
    $typeslist = array(
        array('id'=>1,'name'=>'应用市场'),
        array('id'=>2,'name'=>'发行'),
    );
    return $typeslist;
}
//读取支付类型
function get_paytype_list(){
    $typeslist = array(
        array('id'=>1,'name'=>'支付宝'),
        array('id'=>2,'name'=>'微信'),
        array('id'=>3,'name'=>'钱包'),
        array('id'=>4,'name'=>'银行卡'),
    );
    return $typeslist;
}
//读取商家列表
function get_shoptype_list(){
    $typelist = \think\facade\Db::name('shop_types')->where('delete_time',0)->order('px asc')->select()->toArray();
    return $typelist;
}

function get_area_list(){
    $arealist=\think\facade\Db::name('citys')->field('id,name,code')->where("tid=0")->order('id asc')->select();
    return $arealist;
}
//读取游戏渠道列表
function get_game_channellist()
{
    $channellist = Db::connect('guangying')->name('GameChannel')->field('id,channel_name')->where('is_delete',0)->order('id asc')->select()->toArray();
    //Log::info('channellist='.json_encode($channellist));
    return $channellist;
}


//读取商品分类列表
function get_goods_cate()
{
    $cate = Db::name('GoodsCate')->order('create_time asc')->select()->toArray();
    return $cate;
}

//访问按小时归档统计
function hour_document($arrData)
{
    $documents = array();
    $hour = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
    foreach ($hour as $val) {
        $documents[$val] = 0;
    }
    foreach ($arrData as $index => $value) {
        $archivesTime = intval(date("H", $value['create_time']));
        $documents[$archivesTime] += 1;
    }
    return $documents;
}

//访问按日期归档统计
function date_document($arrData)
{
    $documents = array();
    foreach ($arrData as $index => $value) {
        $archivesTime = date("Y-m-d", $value['create_time']);
        if (empty($documents[$archivesTime])) {
            $documents[$archivesTime] = 1;
        } else {
            $documents[$archivesTime] += 1;
        }
    }
    return $documents;
}

/**
 * 管理员操作日志
 * @param string $type 操作类型 login add edit view delete
 * @param int    $param_id 操作类型
 * @param array  $param 提交的参数
 */
function add_log($type, $param_id = '', $param = [])
{
	$action = '未知操作';
	$type_action = get_config('log.admin_action');
	if($type_action[$type]){
		$action = $type_action[$type];
	}
    if ($type == 'login') {
        $login_admin = Db::name('Admin')->where(array('id' => $param_id))->find();
    } else {
        $session_admin = get_config('app.session_admin');
        $login_admin = \think\facade\Session::get($session_admin);
    }
    $data = [];
    $data['uid'] = $login_admin['id'];
    $data['nickname'] = $login_admin['nickname'];
    $data['type'] = $type;
    $data['action'] = $action;
    $data['param_id'] = $param_id;
    $data['param'] = json_encode($param);
    $data['module'] = strtolower(app('http')->getName());
    $data['controller'] = strtolower(app('request')->controller());
    $data['function'] = strtolower(app('request')->action());
    $parameter = $data['module'] . '/' . $data['controller'] . '/' . $data['function'];
    $rule_menu = Db::name('AdminRule')->where(array('src' => $parameter))->find();
	if($rule_menu){
		$data['title'] = $rule_menu['title'];
		$data['subject'] = $rule_menu['name'];
	}
	else{
		$data['title'] = '';
		$data['subject'] ='系统';
	}
    $content = $login_admin['nickname'] . '在' . date('Y-m-d H:i:s') . $data['action'] . '了' . $data['subject'];
    $data['content'] = $content;
    $data['ip'] = app('request')->ip();
    $data['create_time'] = time();
    Db::name('AdminLog')->strict(false)->field(true)->insert($data);
}


function getRandomString($len, $chars=null)
{
    if (is_null($chars)) {
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    }
    mt_srand(10000000*(double)microtime());
    for ($i = 0, $str = '', $lc = strlen($chars)-1; $i < $len; $i++) {
        $str .= $chars[mt_rand(0, $lc)];
    }
    return strtolower($str);
}

function excelTime($date, $time = false) {
    if(function_exists('GregorianToJD')){
        if (is_numeric( $date )) {
            $jd = GregorianToJD( 1, 1, 1970 );
            $gregorian = JDToGregorian( $jd + intval ( $date ) - 25569 );
            $date = explode( '/', $gregorian );
            $date_str = str_pad( $date [2], 4, '0', STR_PAD_LEFT )
                ."-". str_pad( $date [0], 2, '0', STR_PAD_LEFT )
                ."-". str_pad( $date [1], 2, '0', STR_PAD_LEFT )
                . ($time ? " 00:00:00" : '');
            return $date_str;
        }
    }else{
        $date=(int)$date>25568?(int)$date+1:25569;
        /*There was a bug if Converting date before 1-1-1970 (tstamp 0)*/
        $ofs=(70 * 365 + 17+2) * 86400;
        $date = date("Y-m-d",($date * 86400) - $ofs).($time ? " 00:00:00" : '');
    }
    return $date;
}
//求和
function getSum($array){
    $sum = 0;
    foreach ($array as $v) {
        $sum += $v;
    }
    return $sum;
}